import {
	DERedirectViewActionType,
	IDERedirectViewAbility,
	IDERedirectViewController,
	IDERedirectViewStore,
	IParam,
} from '@/core/interface';
import { router } from '@/router';
import { getIndexRoutePath } from '@/utils';
import { ViewController } from './view-controller';

/**
 * 实体重定向视图控制器
 *
 * @export
 * @class DERedirectViewController
 * @extends {ViewController}
 */
export class DERedirectViewController
	extends ViewController<
		DERedirectViewActionType,
		IDERedirectViewStore,
		IDERedirectViewAbility
	>
	implements
		IDERedirectViewController<IDERedirectViewStore, IDERedirectViewAbility>
{
	/**
	 * 视图加载
	 *
	 * @param {IParam} [opts={}]
	 * @return {*}
	 * @memberof IndexViewController
	 */
	public async viewMounted(opts: IParam = {}) {
		super.viewMounted(opts);
		const entityDetails = App.getAppEntityConfig();
		const { entityCodeName } = this.model;
		if (entityCodeName) {
			const UIService = await App.getUIService(
				entityCodeName,
				this.store.context
			);
			const srfkey = this.store.context[entityCodeName?.toLowerCase()];
			UIService.getAppRDView(
				this.store.context,
				this.store.viewParams,
				{ [entityCodeName.toLowerCase()]: srfkey },
				{ isEnableWorkflow: false }
			).then((response: any) => {
				if (response && response.viewName && response.srfappde) {
					const { currentRoute, push } = router;
					const indexPath = getIndexRoutePath(currentRoute.value);
					let routePath: string = `${indexPath}/${
						response.srfappde
					}/${srfkey}/views/${response.viewName.toLowerCase()}`;
					push(routePath);
				} else {
					App.getNotificationHelper().error(
						App.ts('app.notificationtitle.error', '错误'),
						App.ts('app.common.nofindredirected', '未查找到重定向视图')
					);
				}
			});
		}
	}
}
